package com.cheetah.handler;

import com.cheetah.protocol.codec.CheetahClientDecoder;
import com.cheetah.protocol.codec.CheetahEncoder;
import com.cheetah.protocol.codec.CheetahHeartbeatEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class ClientHandlerInitalizer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private ClientHandler clientHandler;

    @Autowired
    private ReconnectHandler reconnectHandler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ch.pipeline()
                .addLast("reconnect",reconnectHandler)
                .addLast("idle", new IdleStateHandler(0, 0, 60*4 + 30, TimeUnit.SECONDS))
                .addLast(new CheetahClientDecoder())
                .addLast(new CheetahHeartbeatEncoder())
                .addLast(new CheetahEncoder())
                .addLast("client",clientHandler)
                .addLast("heartbeat",new HeartBeatClientHandler());
    }


}
